define(function(require, exports){
	var pubjs  = require('pubjs');
	var util   = require('util');
	var view   = require('@base/view');
	var $      = require('jquery');
	var dspUtil = require('dspUtil');
	var common = require('modules/common');

	// 媒体广告位
	var MediaSpot = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				view_model: {
				}
			});

			// 缓存后端返回来的ids
			this.$ids = [];
			this.$channel = []
			this.$data = null;

			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var self = this;
			var el = self.getDOM();

			$([
				'<div class="pd20">',
					'<span pub-name="importBtn" pub-mod="@base/common/input.button"/>',
					'<span pub-name="deleteBtn" pub-mod="@base/common/input.button"/>',
					'<div class="mt15" pub-name="adList" pub-mod="@base/grid.baseNoDate"/>',
				'</div>'
			].join('')).appendTo(el);

			self.buildFromTemplate({
				deleteBtn: {
					'class': 'btn btn-danger mr10',
					'value': LANG('删除所有')
				},
				importBtn: {
					'class': 'btn btn-default mr10',
					'value': LANG('导入广告位')
				},
				// 广告位列表
				adList: {
					'cols': [
						{type: 'id', width: 80},
						{name: '_id', text: LANG('媒体广告位ID'), align: 'left'},
						{name: 'Name', text: LANG('广告位名称'), align: 'left'},
						{name: 'AdxId', text: LANG('渠道'), align: 'left', render: 'renderChannel'},
						{name: 'Size', text: LANG('广告位尺寸'), align: 'left'},
						{type: 'op', width: 50, html:'<a href="#" data-op="remove">' + LANG('删除') + '</a>'}
					],
					'opClick': true,
					'default_sort': false,
					'hasSearch': false,
					'hasSubGrid': false,
					'hasAmount': false,
					'hasTab': false,
					'hasExport': false,
					'pager': {'size': 10},
					'url':'spot/listByIds',
					'auto_load': false,
					'reqType': 'websocket'
				}
			});

			this.createAsync('adPopwin', 'promotion/popwin/adImport.adImport');
		}
		,afterBuildTemplate: function(){
			if(this.$data){
				this.setData(this.$data);
			}
		}
		,renderChannel: function(val, data){
			var adxIds = pubjs.config('exchange');
			if (data) {
				this.$channel.push(data);
				for (var i = 0; i<adxIds.length; i++) {
					var item = adxIds[i];
					if (+data === +item.id) {
						return item.name;
					}
				}
			}
			else {
				return this;
			}
		}
		,onInputClick: function(ev){
			var name = ev.name;
			switch (name){
				case 'deleteBtn':
					this.deleteAll();
					break;
				case 'importBtn':
					this.$.adPopwin.reset().show();
					break;
			}
		}
		,onAdImportOk: function(ev){
			var cs = this.$;
			var ids = util.clone(this.$ids);
			var data = ev.param.items;
			var currentIds = [];

			data.forEach(function(item){
				var id = item._id.toString ? item._id.toString() : item._id;
				currentIds.push(id);
			});

			// 直接用将两个数组用concat的话，只能合并，不能去重，因为他们的_id不是数字，是int64
			if (currentIds) {
				for (var i = currentIds.length-1; i>=0; i--) {
					var currentId = currentIds[i];
					for (var j = 0; j<ids.length; j++) {
						var idsString = ids[j].toString ? ids[j].toString() : ids[j];
						if (currentId === idsString) {
							currentIds.splice(i, 1);
							break;
						}
					}
				}
				this.$ids = currentIds.concat(ids);
			}
			else{
				pubjs.alert(LANG('找不到相应的广告位'));
				return false;
			}
			// 表格再次请求接口spot/listByIds，以便分页
			this.$channel = [];
			cs.adList.setParam({'ids': this.$ids.join(',')}).load();

			return false;
		}
		,deleteAll: function(){
			var cs = this.$;

			// 清除缓存的ids
			this.$ids = [];
			this.$channel = [];
			this.$.adList.setParam({'ids': []}, true).load();
			return this;
		}
		,getData: function(){
			return this.$ids;
		}
		,setData: function(data){
			this.$data = data;

			if(data && this.get('adList')){
				var self = this;
				var cs = self.$;
				// 更新缓存的已导入的数据
				this.$ids = data;
				this.$channel = [];
				cs.adList.setParam({'ids': data}, true).load();
			}

			return self;
		}
		,reset: function(){
			var cs = this.$;
			// 重置缓存的导入的数据
			this.$ids = [];
			// 表格数据重置会空
			cs.adList.setParam({'ids': []}, true).setData([]);
			return this;
		}
		,onListOpClick: function(ev){
			var param = ev.param;
			var op = param.op;
			if (op === 'remove') {
				// 删除单条记录
				var index = param.index;
				this.$.adList.removeRow(index);
				// 删除缓存里面对应的那条数据
				var data = param.data;
				var id = data._id.toString ? data._id.toString() : data._id;
				var ids = util.clone(this.$ids);
				for (var i = 0; i<ids.length; i++) {
					var item = ids[i].toString ? ids[i].toString() : ids[i];
					if (id === item) {
						ids.splice(i, 1);
						break;
					}
				}

				this.$ids = ids;
				this.$channel = [];
				this.$.adList.setParam({'ids': this.$ids.join(',')}).load();
			}

			return false;
		}
	});
	exports.mediaSpot = MediaSpot;

	// 域名
	var Domain = view.container.extend({
		init: function(config){
			config = pubjs.conf(config, {
				view_model: {
				}
			});

			this.$data = [];

			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var self = this;
			var el = self.getDOM();

			$([
				'<div class="pd20">',
					'<span pub-name="importBtn" pub-mod="@base/common/input.button"/>',
					'<span pub-name="deleteBtn" pub-mod="@base/common/input.button"/>',
					'<div class="mt15" pub-name="adList" pub-mod="@base/grid.baseNoDate"/>',
				'</div>'
			].join('')).appendTo(el);

			self.buildFromTemplate({
				deleteBtn: {
					'class': 'btn btn-danger mr10',
					'value': LANG('删除所有')
				},
				importBtn: {
					'class': 'btn btn-default mr10',
					'value': LANG('导入域名')
				},
				adList: {
					'cols': [
						{type: 'id', width: 80},
						{name: 'Content', text: LANG('内容'), align: 'left', render: 'renderContent'},
						{type: 'op', width: 50, html:'<a href="#" data-op="remove">' + LANG('删除') + '</a>'},
					],
					'opClick': true,
					'default_sort': false,
					'hasSearch': false,
					'hasSubGrid': false,
					'hasAmount': false,
					'hasTab': false,
					'hasExport': false,
					'hasPager': false,
					'auto_load': false,
					'reqType': 'websocket'
				}
			});

			this.createAsync('importPop', 'admin/blacklist/popwin/import.domainImport');
		}
		,afterBuildTemplate: function(){
		}
		,renderContent: function(val, data){
			return data
		}
		,onInputClick: function(ev){
			var name = ev.name;
			switch (name){
				case 'deleteBtn':
					this.deleteAll();
					break;
				case 'importBtn':
					this.$.importPop.reset().show();
					break;
			}
		}
		,onAdImportOk: function(ev){
			var cs = this.$;
			var data = ev.param;

			this.$data = this.$data.concat(data);
			cs.adList.setData(this.$data);

			return false;
		}
		,onListOpClick: function(ev){
			var param = ev.param;
			var op = param.op;
			if (op === 'remove') {
				// 删除单条记录
				var index = param.index;
				this.$.adList.removeRow(index);
			}
			return false;
		}
		,deleteAll: function(){
			var cs = this.$;
			cs.adList.setData([]);
			// 清除缓存的ids
			this.$data = [];

			return this;
		}
		,getData: function(){
			return this.$data;
		}
		,setData: function(data){
			if(data && this.get('adList')){
				var cs = this.$;
				var newData = [];
				for (var i = 0; i < data.length; i++) {
					newData.push({Content: data[i]});
				}
				cs.adList.setData(newData);
				// 更新缓存的已导入的数据
				this.$data = newData;
			}

			return this;
		}
		,reset: function(){
			var cs = this.$;
			// 重置缓存的导入的数据
			this.$data = [];
			// 表格数据重置会空
			cs.adList.setData([]);
			return this;
		}
	});
	exports.domain = Domain;

	// APPID
	var Appid = Domain.extend({
		init: function(config){
			config = pubjs.conf(config, {
				view_model: {
				}
			});

			this.$data = [];

			this.Super('init', arguments);
		}
		,afterBuild: function(){
			var self = this;
			var el = self.getDOM();

			$([
				'<div class="pd20">',
					'<span pub-name="importBtn" pub-mod="@base/common/input.button"/>',
					'<span pub-name="deleteBtn" pub-mod="@base/common/input.button"/>',
					'<div class="mt15" pub-name="adList" pub-mod="@base/grid.baseNoDate"/>',
				'</div>'
			].join('')).appendTo(el);

			self.buildFromTemplate({
				deleteBtn: {
					'class': 'btn btn-danger mr10',
					'value': LANG('删除所有')
				},
				importBtn: {
					'class': 'btn btn-default mr10',
					'value': LANG('导入APPID')
				},
				// 广告位列表
				adList: {
					'cols': [
						{type: 'id', width: 80},
						{name: 'Content', text: LANG('内容'), align: 'left', render: 'renderContent'},
						{type: 'op', width: 50, html:'<a href="#" data-op="remove">' + LANG('删除') + '</a>'},
					],
					'opClick': true,
					'default_sort': false,
					'hasSearch': false,
					'hasSubGrid': false,
					'hasAmount': false,
					'hasTab': false,
					'hasExport': false,
					'hasPager': false,
					// 'url':'spot/listByIds',
					'auto_load': false,
					'reqType': 'websocket'
				}
			});

			this.createAsync('importPop', 'admin/blacklist/popwin/import.appidImport');
		}
	});
	exports.appid = Appid;
});